package com.yycx.module.bus.provider.service.hy.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.security.OpenHelper;
import com.yycx.module.bus.client.entity.BusDept;
import com.yycx.module.bus.client.entity.hy.BusDeptHy;
import com.yycx.module.bus.client.entity.BusSaleman;
import com.yycx.module.bus.provider.mapper.hy.BusDeptHyMapper;
import com.yycx.module.bus.provider.service.hy.BusDeptHyService;
import com.yycx.common.mybatis.base.service.impl.BaseServiceImpl;
import com.yycx.common.mybatis.model.ResultBody;
import org.springframework.stereotype.Service;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.mybatis.query.CriteriaQuery;
import org.springframework.transaction.annotation.Propagation;
import com.yycx.common.mybatis.query.CriteriaSave;
import org.springframework.transaction.annotation.Transactional;

/**
 * 海鹰部门映射表接口实现类
 *
 * @author flyme
 * @date 2023-09-22
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BusDeptHyServiceImpl extends BaseServiceImpl<BusDeptHyMapper, BusDeptHy> implements BusDeptHyService {

    @Override
    public ResultBody beforeAdd(CriteriaSave cs, BusDeptHy bdh, EntityMap extra) {
        return ResultBody.ok();
    }


    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody beforePageList(CriteriaQuery<BusDeptHy> cq, BusDeptHy bdh, EntityMap requestMap) {
        cq.clear();
        String deptName = cq.getParams("deptName");
        String deptType = cq.getParams("deptType");
        cq.select(BusDeptHy.class, "deptHyId","hyDeptNo");
        cq.select(BusDept.class, "deptId","deptName","deptCode","invoiceRate","deptType");
        cq.select(BusSaleman.class,"saleManName");
        cq.eq(BusDept.class, "deptState", CommonConstants.INT_1);
        if (FlymeUtils.isNotEmpty(deptName)) {
            cq.and(e -> e.like("dept.deptName", deptName).or().like("dept.deptCode", deptName));
        }
        cq.eq(FlymeUtils.isNotEmpty(deptType), "dept.deptType", deptType);
        cq.eq(BusDeptHy.class, "companyId", OpenHelper.getCompanyId());
        cq.createJoin(BusDept.class).setMainField("deptId");
        cq.createJoin(BusSaleman.class).setMainAlias("dept").setMainField("saleManId").setJoinAlias("saleman").setJoinField("saleManId");
        cq.orderByAsc("dept.deptCode");
        return ResultBody.ok();
    }

    @Override
    public BusDeptHy getByHyDeptId(String hyDeptNo, Long companyId) {
        return getOne(new QueryWrapper<BusDeptHy>().lambda().eq(true, BusDeptHy::getCompanyId, companyId).eq(BusDeptHy::getHyDeptNo, hyDeptNo));
    }

    @Override
    public BusDeptHy getByDeptCode(String deptCode, Long companyId) {
        return getOne(new QueryWrapper<BusDeptHy>().lambda().eq(true, BusDeptHy::getCompanyId, companyId).eq(BusDeptHy::getHyDeptCode, deptCode));
    }
}
